package zsd.reiview.utility;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import zsd.review.common.Option;


public class FileUtility extends Utility {

	
	/**
	 * @param link
	 * @param path
	 * @param bool
	 * @throws IOException
	 * @description 将字符串link 安格式写入 lda初始文件
	 */
	public static void WriteList(List<String> link, String path, boolean bool) {

		try {
			File file = new File(path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			
			String tempstr = "";
			BufferedWriter writer = null;
			writer = new BufferedWriter(new FileWriter(path, bool));
			Iterator<String> itr = link.iterator();
			while (itr.hasNext()) {
				writer.write(itr.next());
				//writer.write("\n");
				writer.write(Option.LineMark());
			}
			writer.flush();
			writer.close();
		} catch (IOException e) {
			   e.printStackTrace();  
			    
		}
		
	}
	
	public static List<String> read2List(String path) {
		List<String> list = new ArrayList<String>();
		try {
			File file = new File(path);
			if (!file.exists()) {
				System.err.println("file" + path + "is not exits! creat! ");
				System.err.println(path);
				System.out.println();

				if (!file.getParentFile().exists())
					file.getParentFile().mkdirs();
				else
					file.createNewFile();
			}

			String tempStr = null;
			BufferedReader reader = new BufferedReader(new FileReader(path));
			StringBuffer str11 = new StringBuffer();
			while ((tempStr = reader.readLine()) != null) {
				list.add(tempStr);
				str11.append(tempStr);
			}
			reader.close();
			return list;
		} catch (Exception e) {
			return list;
			// TODO: handle exception
		}

	}

	public static String readStr(String path) throws IOException {
		File file = new File(path);
		if (!file.exists()) {
			System.err.println("file" + path + "is not exits! creat! ");
			System.err.println(path);
			System.out.println();

			if (!file.getParentFile().exists())
				file.getParentFile().mkdirs();
			else
				file.createNewFile();
		}

		String tempStr = null;
		BufferedReader reader = new BufferedReader(new FileReader(path));
		StringBuffer str11 = new StringBuffer();
		while ((tempStr = reader.readLine()) != null) {

			str11.append(tempStr);
		}
		reader.close();
		String string = str11.toString();
		String newString = new String(string.getBytes(), "UTF-8");
		// String newString = new String(string.getBytes(),"gbk");
		return string;
	}
	
	public static void createFile(String path) throws IOException{
			File file = new File(path);
	if (!file.exists()) {
		System.err.println("file:   " + path + "   is not exits!   creat! ");
		//System.err.println(path);
		System.out.println();

		if (!file.getParentFile().exists())
			file.getParentFile().mkdirs();
		else
			file.createNewFile();
	}
		
	}

	public static String readStr(String path, String encode1, String encode2) throws IOException {
		File file = new File(path);
		if (!file.exists()) {
			System.err.println("file" + path + "is not exits! creat! ");
			System.err.println(path);
			System.out.println();

			if (!file.getParentFile().exists())
				file.getParentFile().mkdirs();
			else
				file.createNewFile();
		}

		String tempStr = null;
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encode1));
		StringBuffer str11 = new StringBuffer();
		while ((tempStr = reader.readLine()) != null) {
			// String str2 = new String(tempStr.getBytes(encode1), encode2);
			str11.append(tempStr);
		}
		reader.close();
		String string = str11.toString();
		String newString = new String(string.getBytes(), encode2);
		// String newString = new String(string.getBytes(),"gbk");
		return newString;
	}

	public static void writeString(String path, String str) throws IOException {
		File file = new File(path);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		if (file.exists()) {
			file.delete();
		}

		BufferedWriter writer = null;

		writer = new BufferedWriter(new FileWriter(path));

		String utfStr = new String(str.getBytes(), zsd.review.common.Optional.CHARSET);
		writer.write(utfStr);
		writer.close();
	}

	public static void addWriteString(String path, String str) throws IOException {
		File file = new File(path);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}

		BufferedWriter writer = null;

		writer = new BufferedWriter(new FileWriter(path, true));
		writer.write(str);
		writer.write(Option.LineMark());
		writer.close();
	}

	public static File getChildFileFromDir(String dir, int num) throws IOException {
		File file = new File(dir);
		if (!file.isDirectory()) {
			System.err.println("dir" + dir + "is not dir ");
			System.err.println(dir);
			System.out.println();
		}
		if (!file.exists()) {
			System.err.println("dir" + dir + "is not exits! creat! ");
			System.err.println(dir);
			System.out.println();
			file.mkdirs();
		}
		File[] child = file.listFiles();
		if (child.length > 0)
			return child[0];
		File child1 = new File(dir + num + ".txt");
		child1.createNewFile();
		return child1;
	}

	public static void convertEncode(String path, String encode1, String encode2) {
		try {
			File file = new File(path);
			if (!file.exists()) {
				System.err.println("file" + path + "is not exits! creat! ");
				System.err.println(path);
				System.out.println();

				if (!file.getParentFile().exists())
					file.getParentFile().mkdirs();
				else
					file.createNewFile();
			}

			String tempStr = null;
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encode1));
			StringBuffer str11 = new StringBuffer();
			BufferedWriter writer = null;

			writer = new BufferedWriter(new FileWriter(path + "_" + encode2));
			while ((tempStr = reader.readLine()) != null) {
				// String str2 = new String(tempStr.getBytes(encode1), encode2);
				str11.append(tempStr);
				String str2 = new String(tempStr.getBytes(), encode2);
				writer.write(str2);
				writer.write(Option.LineMark());
			}
			reader.close();
			writer.close();
			String string = str11.toString();
			String newString = new String(string.getBytes(), encode2);
			// String newString = new String(string.getBytes(),"gbk");
			return;
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
	}

	public static void main(String arg[]) {
		FileUtility.convertEncode("G:\\1\\COAE2011Answer\\task3.txt", "gbk", "utf-8");
	}

}
